package com.baitiaojun.common.utils.convert;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.stream.Collectors;

/**
 * 转换参数类型
 */
@Slf4j
public class ParamConvertUtils {

    /**
     * 单个参数转为指定类型次
     * @param attribute
     * @param defaultValue
     * @return
     */
    public static Long convertLong(Object attribute, Long defaultValue) {
        if (attribute == null) {
            return defaultValue;
        }
        if (attribute instanceof Long) {
            return (Long) attribute;
        }
        if (attribute instanceof Number) {
            return ((Number) attribute).longValue();
        }
        if (attribute instanceof String) {
            String strVal = convertStr(attribute);
            if (strVal.isEmpty()) {
                return defaultValue;
            }
        }
        return defaultValue;
    }

    /**
     * json参数转为对象
     * @param clazz
     * @param defaultValue
     * @return
     * @param <T>
     */
    public static <T> T convertToObject(BufferedReader reader, Class<T> clazz, T defaultValue) {
        try {
            if (reader == null) {
                return defaultValue;
            }
            String jsonStr = reader.lines().collect(Collectors.joining());
            if (StringUtils.isEmpty(jsonStr)) {
                return defaultValue;
            }
            return JSON.parseObject(jsonStr, clazz);
        } catch (Exception e) {
            log.warn("json参数转换失败{}", e.getMessage());
            return defaultValue;
        }
    }

    public static String convertStr(Object attribute) {
        return convertStr(attribute, null);
    }

    public static String convertStr(Object attribute, String defaultValue) {
        if (null == attribute) {
            return defaultValue;
        }
        if (attribute instanceof String) {
            return (String) attribute;
        }
        return attribute.toString();
    }

    public static Long convertLong(Object attribute) {
        return convertLong(attribute, null);
    }

    public static Integer convertInteger(Object attribute) {
        return convertInteger(attribute, null);
    }

    public static Integer convertInteger(Object attribute, Integer defaultValue) {
        if (attribute == null) {
            return defaultValue;
        }
        if (attribute instanceof Integer) {
            return (Integer) attribute;
        }
        if (attribute instanceof Number) {
            return ((Number) attribute).intValue();
        }
        if (attribute instanceof String) {
            String strVal = convertStr(attribute);
            if (strVal.isEmpty()) {
                return defaultValue;
            }
        }
        return defaultValue;
    }

    public static Double convertDouble(Object attribute) {
        return convertDouble(attribute, null);
    }

    public static Double convertDouble(Object attribute, Double defaultValue) {
        if (attribute == null) {
            return defaultValue;
        }
        if (attribute instanceof Double) {
            return (Double) attribute;
        }
        if (attribute instanceof Number) {
            return ((Number) attribute).doubleValue();
        }
        if (attribute instanceof String) {
            String strVal = convertStr(attribute);
            if (strVal.isEmpty()) {
                return defaultValue;
            }
        }
        return defaultValue;
    }

    public static Float convertFloat(Object attribute) {
        return convertFloat(attribute, null);
    }

    public static Float convertFloat(Object attribute, Float defaultValue) {
        if (attribute == null) {
            return defaultValue;
        }
        if (attribute instanceof Float) {
            return (Float) attribute;
        }
        if (attribute instanceof Number) {
            return ((Number) attribute).floatValue();
        }
        if (attribute instanceof String) {
            String strVal = convertStr(attribute);
            if (strVal.isEmpty()) {
                return defaultValue;
            }
        }
        return defaultValue;
    }

    public static Boolean convertBoolean(Object attribute) {
        return convertBoolean(attribute, null);
    }

    public static Boolean convertBoolean(Object attribute, Boolean defaultValue) {
        if (attribute == null) {
            return defaultValue;
        }
        if (attribute instanceof Boolean) {
            return (Boolean) attribute;
        }
        String strVal = convertStr(attribute);
        if (strVal.isEmpty()) {
            return defaultValue;
        }
        strVal = strVal.trim().toLowerCase();
        switch (strVal) {
            case "true":
                return true;
            case "false":
                return false;
            case "yes":
                return true;
            case "ok":
                return true;
            case "no":
                return false;
            case "1":
                return true;
            case "0":
                return false;
            default:
                return defaultValue;
        }
    }

    public static String convertJsonStr(Object[] args) {
        if (ObjectUtil.isNotEmpty(args)) {
            return com.baitiaojun.common.utils.string.StringUtils.EMPTY;
        }
        StringBuilder builder = new StringBuilder(args.length);
        for (Object arg : args) {
            if (arg != null) {
                ObjectMapper objectMapper = new ObjectMapper();
                try {
                    String jsonStr = objectMapper.writeValueAsString(arg);
                    JsonNode jsonNode = objectMapper.readTree(jsonStr);
                    Iterator<String> iterator = jsonNode.fieldNames();
                    while (iterator.hasNext()) {
                        String fieldName = iterator.next();
                        JsonNode fieldValue = jsonNode.get(fieldName);
                        builder.append(fieldName)
                                .append(":")
                                .append(fieldValue)
                                .append("\n");
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return builder.toString();
    }
}
